<template>
  <div class="class_children">
    <div>
      <div>
        <van-nav-bar
          title="标题"
          right-text="搜索"
          left-arrow
          @click-left="onClickLeft"
          @click-right="onClickRight"
        />
      </div>
      <div class="operation">
        <div @click="sortID">全部</div>
        <div @click="sortPrice">价格</div>
        <div>筛选</div>
      </div>
      <van-grid :column-num="2" :border="false">
        <van-grid-item v-for="(item, index) in searchList" :key="index">
          <div @click="todetails(item.id)">
            <div>
              <van-image :src="item.shop_image"></van-image>
            </div>
            <div>
              <span style="color:#ff464e; font-size:16px;">￥{{item.shop_price}}</span>
              <span
                style="color:#bbb; font-size:10px;text-decoration: line-through;"
              >￥{{item.shop_old_price}}</span>
              <br />
              <p class="title-p">{{item.shop_name}}</p>
            </div>
          </div>
        </van-grid-item>
      </van-grid>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchList: [],
      descarr: [],
      ascarr: []
    };
  },
  created() {
    this.$toast.loading({
      message: "加载中...",
      forbidClick: true
    });
    setTimeout(() => {
      this.getChild(this.$route.query.id);
      this.ascPrice(this.$route.query.id);
      this.descPrice(this.$route.query.id);
    }, 3000);
    setTimeout(() => {
      if (this.searchList.length == 0) {
        this.$router.push("/");
      }
    }, 4000);
  },
  methods: {
    // 首次获取数据
    getChild(id) {
      this.$http.get("/class_children", { params: { id: id } }).then(res => {
        this.searchList = res.data.data;
      });
    },
    // 全部(id)
    sortID() {
      this.getChild(this.$route.query.id);
    },
    // 倒序
    descPrice(id) {
      this.$http
        .get("/class_children_asc", { params: { id: id } })
        .then(res => {
          this.descarr = res.data.data;
        });
    },
    // 正序
    ascPrice(id) {
      this.$http
        .get("/class_children_desc", { params: { id: id } })
        .then(res => {
          this.ascarr = res.data.data;
        });
    },
    // 价格
    sortPrice() {
      if (this.searchList == this.ascarr) {
        this.searchList = this.descarr;
      } else if (this.searchList == this.descarr) {
        this.searchList = this.ascarr;
      } else {
        this.searchList = this.descarr;
      }
    },
    // 跳转到商品详情页
    todetails(id) {
      this.$router.push({ path: "/shopdetails", query: { id: id } });
    },
    // 返回
    onClickLeft() {
      this.$router.push("/class");
    },
    // 搜索
    onClickRight() {
      this.$router.push("/start_search");
    }
  }
};
</script>

<style lang="less">
.class_children {
  .van-icon {
    color: #333;
  }
  .van-nav-bar__text {
    color: #333;
  }
  .back {
    position: relative;
    i {
      position: absolute;
      top: 18px;
      left: 6px;
    }
  }
  .defeated {
    text-align: center;
  }
  .shop {
    background-color: #f4f4f8;
  }
  .title {
    margin-left: 10px;
    color: #666;
    font-size: 12px;
  }
  .operation {
    margin: 0 10px;
    overflow: hidden;
    border-bottom: 1px dashed #999;
    div {
      width: 33%;
      height: 30px;
      float: left;
      text-align: center;
      line-height: 30px;
    }
  }
  .btntop {
    background-color: #fff;
    border-radius: 50%;
    box-shadow: 0px -1px 3px #888 inset;
  }
  .title-p {
    color: #333;
    overflow: hidden; /*超出部分隐藏*/
    text-overflow: ellipsis; /* 超出部分显示省略号 */
    white-space: nowrap; /*规定段落中的文本不进行换行 */
    width: 150px; /*需要配合宽度来使用*/
  }
}
</style>